#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class AuditionOnline_rgm(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(AuditionOnline_rgm,self).__init__("RGM")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):
        
        name = []
        char = self.inFile.read_char()
        while char != "\x00":
            name.append(char)
            char = self.inFile.read_char()
        return ''.join(name)
    
    def parse_vertices(self, meshName, numVerts):
        
        for i in xrange(numVerts):
            vx, vy, vz, nx, ny, nz, tu, tv = self.inFile.read_float(8)
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
        
    def parse_faces(self, meshName, numFaces):
        
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        idstring = self.inFile.read_string(31, "\x00")
        self.inFile.seek(10, 1)
        
        meshName = self.read_name()
        self.create_object(meshName)
        numMesh = self.inFile.read_ubyte()
        
        print "%d mesh" %numMesh
        for i in xrange(numMesh):
            print self.read_name()
            self.inFile.read_ubyte()
            
        unk1, unk2 = self.inFile.read_long(2)
        self.inFile.seek(53, 1)
        
        #for i in xrange(numMesh):
            #numVerts = self.inFile.read_long()
            #print "%d vertices" %numVerts
            #self.inFile.read_byte()
            #self.parse_vertices(meshName, numVerts)
            
            #numFaces = self.inFile.read_long()
            #print numFaces
            #self.parse_faces(meshName, numFaces)
            #print self.inFile.tell()
            #self.inFile.seek(106, 1)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = AuditionOnline_rgm(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "Delight3D", "RGM", "Audition Online RGM"

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)